package com.example.iot.util;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import java.util.UUID;

/**
 * @author ChenXiangpeng
 */
public class UUIDUtil {
    public static UUID fromString(String uuid){
        try{
            if (StringUtils.isBlank(uuid)){
                return null;
            }
            return UUID.fromString(uuid);
        }catch (IllegalArgumentException e){
            return UUID.fromString(new StringBuilder()
                    .append(uuid, 0, 8)
                    .append(StringPool.DASH)
                    .append(uuid, 8, 12)
                    .append(StringPool.DASH)
                    .append(uuid, 12, 16)
                    .append(StringPool.DASH)
                    .append(uuid, 16, 20)
                    .append(StringPool.DASH)
                    .append(uuid.substring(20)).toString());
        }
    }
}
